package cn.iocoder.yudao.module.promotion.api.shortLinkDetail;

import cn.iocoder.yudao.module.promotion.controller.admin.shortlinkdetail.vo.ShortLinkDetailCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.shortlinkgeneratee.vo.ShortLinkGenerateeCreateReqVO;
import cn.iocoder.yudao.module.promotion.service.shortlinkdetail.ShortLinkDetailService;
import cn.iocoder.yudao.module.promotion.service.shortlinkgeneratee.ShortLinkGenerateeService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.module.promotion.util.PromotionUtils.generateBatchNumber;
import static cn.iocoder.yudao.module.promotion.util.PromotionUtils.generateRandomShortLink;

@Service
@Validated
public class ShortLinkDetailImpl implements ShortLinkDetailApi {
    @Resource
    private ShortLinkGenerateeService shortLinkGenerateeService;

    @Resource
    private ShortLinkDetailService shortLinkDetailService;
    @Override
    public String createShortLinkDetail(String shortDomain, String longUrl, Long phoneNumber) {
        String shortLink = shortDomain + "/" + generateRandomShortLink();
        ShortLinkGenerateeCreateReqVO createReqVO = new ShortLinkGenerateeCreateReqVO();
        createReqVO.setName("未下单提醒[系统创建]");
        createReqVO.setLongUrl(longUrl);
        createReqVO.setShortDomain(shortDomain);
        createReqVO.setStatus((byte) 0);
        Long generateeId = shortLinkGenerateeService.createShortLinkGeneratee(createReqVO);
        ShortLinkDetailCreateReqVO shortLinkDetailCreateReqVO = new ShortLinkDetailCreateReqVO();
        shortLinkDetailCreateReqVO.setShortLinkId(generateeId);
        shortLinkDetailCreateReqVO.setName("未下单提醒[系统创建]");
        shortLinkDetailCreateReqVO.setStatus((byte) 0);
        shortLinkDetailCreateReqVO.setBatchNumber("BATCH-SMS-"+generateBatchNumber());
        shortLinkDetailCreateReqVO.setPhoneNumber(phoneNumber);
        shortLinkDetailCreateReqVO.setShortUrl(shortLink);
        shortLinkDetailCreateReqVO.setStatus((byte) 0);
        shortLinkDetailService.createShortLinkDetail(shortLinkDetailCreateReqVO);
        return shortLink;
    }
}
